package com.sfdk.graphics.ui.parts
{
import com.sfdk.core.GameUtil;
import com.sfdk.graphics.api.IHotkeyListener;
import com.sfdk.graphics.core.SpriteEx;
import com.sfdk.locale.LocalisedMessageEngine;
import com.sfdk.pools.TextFormatPool;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.AntiAliasType;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Timer;

public class TextButton extends SpriteEx implements IHotkeyListener
	{
		private var internalTF_ : Bitmap;
		private var alternateTF_ : Bitmap;
		private var hotkeyValue_ : String;
		
		private var _fadeTimer : Timer;
		private var _timeFrame : int = 30;
		private var _timeCount : int;
		private var _fadeCount : int;
		
		private var _enabled : Boolean = true;
		
		public function TextButton(textValue : String, name : String = "anoTextButton", style : String = "Arial-White-12", underlineOnHover : Boolean = false, skipTranslation : Boolean = false)
		{
			this.name = name;
			useHandCursor = true;
			
			var defaultTF : TextField = GameUtil.DEFAULT_TF;
			if(!skipTranslation)
				defaultTF.text = LocalisedMessageEngine.getMessage(textValue);
			else
				defaultTF.text = textValue;
			defaultTF.antiAliasType = AntiAliasType.ADVANCED;
			defaultTF.autoSize = TextFieldAutoSize.LEFT;
			defaultTF.setTextFormat(TextFormatPool.instance.getTextFormat(style));
			//defaultTF.embedFonts = true;
			
			internalTF_ = new Bitmap(new BitmapData(defaultTF.textWidth + 5, defaultTF.textHeight + 5, true, 0));
			internalTF_.bitmapData.draw(defaultTF, null, null, null, null, true);
			attachChild(internalTF_, 0, 0, "buttonContent");
			
			if(underlineOnHover)
			{
				var textFormat : TextFormat = defaultTF.getTextFormat();
				textFormat.underline = true;
				defaultTF.setTextFormat(textFormat);
				
				alternateTF_ = new Bitmap(new BitmapData(defaultTF.textWidth + 5, defaultTF.textHeight + 5, true, 0));
				alternateTF_.bitmapData.draw(defaultTF, null, null, null, null, true);
				alternateTF_.visible = false;
				attachChild(alternateTF_, 0, 0, "alternateButtonContent");
				
				addEventListener(MouseEvent.ROLL_OVER, onMouseOver, false, 0, true);
				addEventListener(MouseEvent.ROLL_OUT, onMouseOut, false, 0, true);
			}
		}
		
		private var _data : Object;

		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
		}

		
		public function set hotkey (value : String) : void
		{
			hotkeyValue_ = value.toUpperCase();
		}
		
		public function get hotkey () : String
		{
			return hotkeyValue_;
		}
		
		public function fadeAway(time : int) : void
		{
			if(_fadeTimer != null)
			{
				_fadeTimer.stop();
				_fadeTimer.removeEventListener(TimerEvent.TIMER, onFadeProgress);
				_fadeTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, onFadeComplete);
			}
			
			_timeCount = int(time / _timeFrame);
			_fadeTimer = new Timer(_timeFrame, _timeCount);
			
			_fadeTimer.addEventListener(TimerEvent.TIMER, onFadeProgress);
			_fadeTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onFadeComplete);
			_fadeTimer.start();
		}
		// Event Handler
		
		private function onMouseOver(e : MouseEvent) : void
		{
			if(_enabled)
			{
				internalTF_.visible = false;
				alternateTF_.visible = true;
			}
		}
		
		private function onMouseOut(e : MouseEvent) : void
		{
			if(_enabled)
			{
				internalTF_.visible = true;
				alternateTF_.visible = false;
			}
		}
		
		private function onFadeProgress(timerEvent : TimerEvent) : void
		{
			_fadeCount++;
			internalTF_.alpha = Number((_timeCount - _fadeCount) / _timeCount);
		}
		
		private function onFadeComplete(timerEvent : TimerEvent) : void
		{
			dispatchEvent(new Event("textFaded"));
		}

		public function get enabled():Boolean
		{
			return _enabled;
		}

		public function set enabled(value:Boolean):void
		{
			_enabled = value;
			super.mouseEnabled = value;
		}

	}
}